import 'dart:io';
import 'dart:ui';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_learning/isolate/utils/ly_log.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// app基础配置类
class Config {
  // 是否开启debug模式
  static bool isDebug = false;
  static bool isFirstLaunch = true;
  static bool isLogin = false;
  static String deviceId = "";
  static String deviceModel = "";
  static String version = "";
  static String buildNumber = "";
  static String rck = "";
  static String googleTranslationKey = "";
  static bool autoTranslate = true;
  static String packageName = '';
  static String lanCode = '';

//  是否release
  static bool get isRelease => const bool.fromEnvironment("dart.vm.product");

  //项目启动前 初始化功能
  static Future init() async {
    //因为EasyLoading是一个全局单例, 所以你可以在任意一个地方自定义它的样式:
    //初始化状态栏
    initStatusBar();
    //  初始化持久工具
    await getUniqueId();
    SharedPreferences prefs = await SharedPreferences.getInstance();
    autoTranslate = prefs.getBool('autoTranslate') ?? false;
    isFirstLaunch = prefs.getBool('isFirstLaunch') ?? true;
    isLogin = prefs.getBool('isLogin') ?? false;
    if (isFirstLaunch) {
      prefs.setBool("isFirstLaunch", false);
      isFirstLaunch = false;
    }
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    version = packageInfo.version;
    buildNumber = packageInfo.buildNumber;
    packageName = packageInfo.packageName;
    lanCode = window.locale.languageCode;
  }

//  初始化状态栏
  static void initStatusBar() {
    if (Platform.isAndroid) {
      //如果是Android设备，状态栏设置为透明沉浸
      SystemUiOverlayStyle _style = const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        systemNavigationBarColor: Colors.black,
      );
      SystemChrome.setSystemUIOverlayStyle(_style);
    }
    if (Platform.isIOS) {
      /// 显示状态栏
      SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
          overlays: [SystemUiOverlay.top]);
      // /// 隐藏状态栏
      // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
    }
  }

  static Future getUniqueId() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isIOS) {
      IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
      deviceModel = iosDeviceInfo.model ?? '';
      const storage = FlutterSecureStorage();
      deviceId = await storage.read(key: "deviceId") ?? "";
      if (deviceId.isEmpty) {
        deviceId = "${iosDeviceInfo.identifierForVendor}";
        await storage.write(key: "deviceId", value: deviceId);
      }
    } else {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      deviceModel = androidInfo.hardware ?? '';
    }
  }
}
